home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / bs941029.tgz / bbsx-941029.tar / bbsx / bbsrenum.c < prev    next >
C/C++ Source or Header  |  1994-10-29  |  4KB  |  189 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <sys/types.h>
  5. #include <pwd.h>
  6. #include <fcntl.h>
  7.  
  8. #include "bbs.h"
  9. #include "bbs.hd"
  10.  
  11. #define PASSWORD "/etc/passwd"
  12.  
  13.  
  14. const struct cmdtable cmdtable[1];
  15.  
  16. struct user_seq {
  17.   int uid;
  18.   int seq;
  19. } ;
  20.  
  21. struct user_seq user_seq[65000];
  22.  
  23. void Make_parent_directories(const char *filename);
  24.  
  25. /*---------------------------------------------------------------------------*/
  26.  
  27. void Make_parent_directories(const char *filename)
  28. {
  29.   char *p, dirname[1024];
  30.  
  31.   strcpy(dirname, filename);
  32.   p = strrchr(dirname, '/');
  33.   if (!p) halt();
  34.   *p = 0;
  35.   if (!mkdir(dirname, 0755)) return;
  36.  
  37.   if (errno == EEXIST) return ;
  38.   if (errno != ENOENT) halt();
  39.   Make_parent_directories(dirname);
  40.   if (mkdir(dirname, 0755)) halt();
  41. }
  42.  
  43.  
  44. /*---------------------------------------------------------------------------*/
  45.  
  46. char *Xgetfilename(int mesg, int *comp)
  47. {
  48.   static char buf[12];
  49.   char cname[50];
  50.   char comm[50];
  51.   FILE *fp;
  52.  
  53.   *comp =0;
  54.   sprintf(buf,
  55.       "%02x/%02x/%02x/%02x",
  56.       (mesg >> 24) & 0xff,
  57.       (mesg >> 16) & 0xff,
  58.       (mesg >>  8) & 0xff,
  59.       (mesg      ) & 0xff);
  60.   strcpy(cname,buf);
  61.   strcat(cname,compressext);   
  62.   if (fp=fopen(cname,"r"))
  63.   {
  64.     fclose(fp);
  65.     *comp=1;
  66.     return cname;
  67.   }      
  68.   return buf;
  69. }
  70.  
  71.  
  72.  
  73. main() {
  74.  
  75.   FILE *pwdfp, *fp, *proto;
  76.   struct passwd *pw;
  77.   struct index index;
  78.   char *p;
  79.   char line[1024];
  80.   int count = 0;
  81.   int new_nr = 1;
  82.   int i,n;
  83.   int is_compressed;
  84.   int old_ind, new_ind;
  85.   char ofilename[1024], nfilename[1024];
  86.  
  87.   if (getuid()) {
  88.     perror("permission denied");
  89.     return 1;
  90.   }  
  91.  
  92.   if (chdir(WRKDIR)) {
  93.     mkdir(WRKDIR, 0755);
  94.     if (chdir(WRKDIR)) halt();
  95.   }
  96.   
  97.   read_config();
  98.   
  99.   if ((pwdfp =fopen(PASSWORD,"r")) == NULL) { 
  100.     perror("error in opening" PASSWORD);
  101.     return;
  102.   }
  103.   
  104.   if ((proto =fopen("renum.proto","w")) == NULL) { 
  105.     perror("error in opening renum.proto");
  106.     return;
  107.   }  
  108.   
  109.   p= __pwdalloc();
  110.   while(pw =__pwdread(pwdfp, p)) {
  111.     sprintf(line,"%s/%s",pw->pw_dir,SEQFILE);
  112.     if (fp=fopen(line,"r")) {
  113.       fread(line,sizeof(line),1,fp);
  114.       user_seq[count].uid = pw->pw_uid;
  115.       user_seq[count].seq = atoi(line);
  116.       count++;
  117.       fclose(fp);
  118.     }  
  119.   }
  120.   
  121.   fclose(pwdfp);
  122.   
  123.   sprintf(line,"%s.o",INDEXFILE);
  124.   
  125.   if(link(INDEXFILE, line) == -1) { 
  126.     perror("can't rename old indexfile");
  127.     return;
  128.   }
  129.   if(unlink(INDEXFILE) == -1) { 
  130.     perror("can't remove old indexfile");
  131.     return;
  132.   }
  133.   
  134.   if ((new_ind = open(INDEXFILE, O_RDWR | O_CREAT, 0644)) < 0) { 
  135.     perror("can't open new indexfile");
  136.     return;
  137.   }
  138.   
  139.   if ((old_ind = open(line, O_RDONLY, 0644)) < 0) { 
  140.     perror("can't open old indexfile");
  141.     return;
  142.   }
  143.   
  144.   for (;;) {
  145.     n=read(old_ind,&index, sizeof(struct index));
  146.     if (n < 1) break;
  147.     if (!(index.flags & DELETED)) {
  148.       strcpy(ofilename,Xgetfilename(index.mesg,&is_compressed));
  149.       Make_parent_directories(getfilename(new_nr));
  150.       strcpy(nfilename,Xgetfilename(new_nr,&n));
  151.       if (is_compressed) strcat(nfilename,compressext);
  152.       if(link(ofilename, nfilename) == -1) { 
  153.         printf("can't rename %s to %s error: %d\n",ofilename, nfilename, 
  154.                 errno);
  155.         return;
  156.       }
  157.       if(unlink(ofilename) == -1) { 
  158.         printf("can't remove %s\n",ofilename);
  159.         return;
  160.       }
  161.       for(i=0; i<= count; i++) 
  162.         if(user_seq[i].seq && user_seq[i].seq <= index.mesg) {
  163.           pw=getpwuid(user_seq[i].uid);
  164.           sprintf(line,"%s/%s",pw->pw_dir,SEQFILE);
  165.           if (fp=fopen(line,"w")) {
  166.             fprintf(fp,"%d\n",new_nr);
  167.             fclose(fp);
  168.             chown(line,pw->pw_uid,pw->pw_gid);
  169.           }
  170.           user_seq[i].seq = 0;
  171.         }
  172.       fprintf(proto,"%d\t%d\n",index.mesg,new_nr);
  173.       index.mesg=new_nr;
  174.       if (write(new_ind, &index, sizeof(index)) != sizeof(index)) {
  175.         perror("can't write to newindexfile");
  176.         return;
  177.       }
  178.       new_nr++;  
  179.     }   
  180.   }
  181.   close(old_ind);
  182.   close(new_ind);
  183.   fclose(proto);
  184.   
  185.   return;
  186. }
  187.     
  188.  
  189.